﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SMSPanasonic.Data;

namespace SMSPanasonic.Business
{
    public partial class pa_WarrantyWaiting
    {
    	#region public properties
		public int WarrantyWattingID { get; set; }
		public string ModelCode { get; set; }
		public string ProductCode { get; set; }
		public int AgencyID { get; set; }
		public string CustomerPhone { get; set; }
		public string WarrantyPackageCode { get; set; }
		public DateTime RegisterDate { get; set; }
		public string WarrantyType { get; set; }
		public bool? IsValid { get; set; }
		public bool? Approved { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public string RegisteredFrom { get; set; }
        public string RegisteredPhone { get; set; }
        public string Description { get; set; }
		#endregion

		#region constructor
		public pa_WarrantyWaiting(){ }

		public pa_WarrantyWaiting(IDataReader rd)
		{
			this.WarrantyWattingID = (int)rd["WarrantyWattingID"];
			this.ModelCode = rd["ModelCode"].ToString();
			this.ProductCode = rd["ProductCode"].ToString();
			this.AgencyID = (int)rd["AgencyID"];
			this.CustomerPhone = rd["CustomerPhone"].ToString();
			this.WarrantyPackageCode = rd["WarrantyPackageCode"].ToString();
			this.RegisterDate = (DateTime)rd["RegisterDate"];
			this.WarrantyType = rd["WarrantyType"].ToString();
			this.IsValid = rd["IsValid"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["IsValid"];
			this.Approved = rd["Approved"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Approved"];
            this.CreatedDate = (DateTime)rd["CreatedDate"];
            this.ModifiedDate = rd["ModifiedDate"].Equals(DBNull.Value) ? (DateTime?)null : (DateTime)rd["ModifiedDate"];
            this.RegisteredFrom = rd["RegisteredFrom"].ToString();
            this.RegisteredPhone = rd["RegisteredPhone"].Equals(DBNull.Value) ? null : rd["RegisteredPhone"].ToString();
            this.Description = rd["Description"].ToString();
		}

        public pa_WarrantyWaiting(DataRow row)
        {
            this.WarrantyWattingID = (int)row["WarrantyWattingID"];
            this.ModelCode = row["ModelCode"].ToString();
            this.ProductCode = row["ProductCode"].ToString();
            this.AgencyID = (int)row["AgencyID"];
            this.CustomerPhone = row["CustomerPhone"].ToString();
            this.WarrantyPackageCode = row["WarrantyPackageCode"].ToString();
            this.RegisterDate = (DateTime)row["RegisterDate"];
            this.WarrantyType = row["WarrantyType"].ToString();
            this.IsValid = row["IsValid"].Equals(DBNull.Value) ? (bool?)null : (bool)row["IsValid"];
            this.Approved = row["Approved"].Equals(DBNull.Value) ? (bool?)null : (bool)row["Approved"];
            this.CreatedDate = (DateTime)row["CreatedDate"];
            this.ModifiedDate = row["ModifiedDate"].Equals(DBNull.Value) ? (DateTime?)null : (DateTime)row["ModifiedDate"];
            this.RegisteredFrom = row["RegisteredFrom"].ToString();
            this.RegisteredPhone = row.IsNull("RegisteredPhone") ? null : row["RegisteredPhone"].ToString();
            this.Description = row["Description"].ToString();
        }
		#endregion

		#region static method
        public static void Insert(string modelCode, string productCode, int agencyID, string customerPhone,
                            string warrantyPackageCode, DateTime registerDate, string warrantyType,
                            bool isValid, bool approved, DateTime createDate, DateTime? modifiedDate,
                            string registeredFrom, string registeredPhone, string description)
        {
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@ModelCode", modelCode),
                new SqlParameter("@ProductCode", productCode),
                new SqlParameter("@AgencyID", agencyID),
                new SqlParameter("@CustomerPhone", customerPhone),
                new SqlParameter("@WarrantyPackageCode", string.IsNullOrEmpty(warrantyPackageCode) ? (object)DBNull.Value : warrantyPackageCode),
                new SqlParameter("@RegisterDate", registerDate),
                new SqlParameter("@WarrantyType", warrantyType),
                new SqlParameter("@IsValid", isValid),
                new SqlParameter("@Approved", approved),
                new SqlParameter("@CreatedDate",createDate),
                new SqlParameter("@ModifiedDate", modifiedDate == null ? (object)DBNull.Value : modifiedDate),
                new SqlParameter("@RegisteredFrom",registeredFrom),
                new SqlParameter("@RegisteredPhone",registeredPhone ?? (object)DBNull.Value),
                new SqlParameter("@Description",description ?? (object)DBNull.Value)
            };

            SqlHelper.ExecuteNonQuery(AppConfig.ConnectionString, CommandType.StoredProcedure, "usp_pa_WarrantyWaiting_Insert", param);
        }

        public static void Insert(DataTable dtSource)
        {
            DBSQLCommand.BulkCopy("pa_WarrantyWaiting", dtSource);
        }

        public static DataTable GetByAutoSendSms(int top)
        {
            DataTable retTable = new DataTable();
            try
            {
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("usp_pa_WarrantyWaiting_GetByAutoSendSms", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 30;
                        cmd.Parameters.AddWithValue("@Top", top);
                        retTable.Load(cmd.ExecuteReader());
                        return retTable;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
		#endregion
	}
}
